package com.xydtech.ecard.merchant.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.merchant.domain.EcardEnterpriseLocation;
import com.xydtech.ecard.merchant.domain.EcardLocationHandover;
import com.xydtech.ecard.merchant.mapper.EcardEnterpriseLocationMapper;
import com.xydtech.ecard.merchant.mapper.EcardLocationHandoverMapper;
import com.xydtech.ecard.merchant.service.IEcardEnterpriseLocationService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片企业场所信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-13 下午4:54
 */
@Service
public class EcardEnterpriseLocationServiceImpl implements IEcardEnterpriseLocationService {

    @Autowired
    private EcardEnterpriseLocationMapper ecardEnterpriseLocationMapper;
    @Autowired
    private EcardLocationHandoverMapper ecardLocationHandoverMapper;
    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 查询电子名片企业场所信息
     *
     * @param id 电子名片企业场所信息主键
     * @return 电子名片企业场所信息
     */
    @Override
    public EcardEnterpriseLocation selectEcardEnterpriseLocationById(Long id, String salt)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        EcardEnterpriseLocation enterpriseLocation = ecardEnterpriseLocationMapper.selectEcardEnterpriseLocationById(id, sysDept.getDeptId());
        if (enterpriseLocation != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(enterpriseLocation.getLocationPhone())) {
                    enterpriseLocation.setLocationPhone(AESEncryptionUtils.decrypt(enterpriseLocation.getLocationPhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return enterpriseLocation;
    }

    /**
     * 查询电子名片企业场所信息列表
     *
     * @param ecardEnterpriseLocation 电子名片企业场所信息
     * @return 电子名片企业场所信息
     */
    @Override
    @DataScope(userAlias = "c", deptAlias = "b")
    public List<EcardEnterpriseLocation> selectEcardEnterpriseLocationList(EcardEnterpriseLocation ecardEnterpriseLocation, String salt)
    {
        List<EcardEnterpriseLocation> enterpriseLocationList = ecardEnterpriseLocationMapper.selectEcardEnterpriseLocationList(ecardEnterpriseLocation);
        if (enterpriseLocationList.size() > 0) {
            for (EcardEnterpriseLocation enterpriseLocation : enterpriseLocationList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(enterpriseLocation.getLocationPhone())) {
                        enterpriseLocation.setLocationPhone(AESEncryptionUtils.decrypt(enterpriseLocation.getLocationPhone(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return enterpriseLocationList;
    }

    /**
     * 批量移交场所信息
     * @param ecardEnterpriseLocationList
     * @param registId
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int allocationEcardEnterpriseLocationInfo(List<EcardEnterpriseLocation> ecardEnterpriseLocationList, Long registId, Long userId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        //转交记录
        List<EcardLocationHandover> ecardLocationHandoverList = new ArrayList<>();
        for(int i = 0; i < ecardEnterpriseLocationList.size(); i++){
            EcardEnterpriseLocation ecardEnterpriseLocation = ecardEnterpriseLocationList.get(i);
            //查询场所信息
            EcardEnterpriseLocation oldEcardEnterpriseLocationInfo = ecardEnterpriseLocationMapper.selectEcardEnterpriseLocationById(ecardEnterpriseLocation.getId(), sysDept.getDeptId());

            EcardLocationHandover ecardLocationHandover = new EcardLocationHandover();
            ecardLocationHandover.setLocationId(ecardEnterpriseLocation.getId());
            ecardLocationHandover.setTransferId(oldEcardEnterpriseLocationInfo.getManagementPersonnelId());
            ecardLocationHandover.setTransferDeptId(oldEcardEnterpriseLocationInfo.getDeptId());
            ecardLocationHandover.setRecipientId(ecardEnterpriseLocation.getManagementPersonnelId());
            ecardLocationHandover.setRecipientDeptId(ecardEnterpriseLocation.getDeptId());
            ecardLocationHandoverList.add(ecardLocationHandover);
        }

        //批量移交场所
        int a = ecardEnterpriseLocationMapper.batchUpdateEcardEnterpriseLocationInfo(ecardEnterpriseLocationList);
        //批量插入移交记录
        int b = ecardLocationHandoverMapper.batchInsertEcardLocationHandover(ecardLocationHandoverList);

        return a > b ? b:a;
    }

    /**
     * 批量启用/停用场所
     * @param ecardEnterpriseLocationList
     * @return
     */
    @Override
    public int batchSwitchOverEcardEnterpriseLocationInfo(List<EcardEnterpriseLocation> ecardEnterpriseLocationList) {
        return ecardEnterpriseLocationMapper.batchSwitchOverEcardEnterpriseLocationInfo(ecardEnterpriseLocationList);
    }
}
